package cn.tedu.hhmc.patient.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.tedu.hhmc.patient.pojo.dto.PatientDTO;
import cn.tedu.hhmc.patient.pojo.dto.PatientRelativesParam;
import cn.tedu.hhmc.patient.pojo.vo.PatientRelativesVO;
import cn.tedu.hhmc.patient.pojo.vo.PatientVO;
import cn.tedu.hhmc.patient_disease_relation.mapper.PatientDiseaseRelationMapper;
import cn.tedu.hhmc.patient_disease_relation.pojo.vo.PatientDiseaseRelationVO;
import cn.tedu.hhmc.patient_disease_relation.service.IPatientDiseaseRelationService;
import cn.tedu.hhmc.patient_disease_relation.service.impl.PatientDiseaseRelationServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.tedu.hhmc.patient.mapper.PatientMapper;
import cn.tedu.hhmc.patient.pojo.entity.Patient;
import cn.tedu.hhmc.patient.service.IPatientService;

/**
 * 病人信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-05
 */
@Slf4j
@Service
public class PatientServiceImpl implements IPatientService 
{
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private IPatientDiseaseRelationService patientDiseaseRelationService;

    /**
     * 查询病人信息
     * 
     * @param id 病人信息主键
     * @return 病人信息
     */
    @Override
    public PatientVO selectPatientById(Long id)
    {
        log.debug("查询病人信息列表业务,参数:{}",id);
        PatientVO patientVO = patientMapper.selectPatientById(id);
        List<PatientDiseaseRelationVO> list = patientDiseaseRelationService.selectPatientDiseaseRelationByPatientId(id);
        String[] diseaseNames = new String[list.size()];
        Long[] diseaseIds = new Long[list.size()];
        for (int j = 0; j < list.size(); j++) {
            diseaseNames[j] = list.get(j).getName();
            diseaseIds[j] = list.get(j).getDiseaseId();
        }
        patientVO.setDisease(diseaseNames);
        patientVO.setDiseaseIds(diseaseIds);
        return patientVO;
    }

    /**
     * 查询病人信息列表
     * 
     * @param patient 病人信息
     * @return 病人信息
     */
    @Override
    public List<PatientVO> selectPatientList(Patient patient) {
        log.debug("查询所有病人信息列表业务,参数:{}",patient);
        List<PatientVO> patientList = patientMapper.selectPatientList(patient);
        for (int i = 0; i < patientList.size(); i++) {
            List<PatientDiseaseRelationVO> list = patientDiseaseRelationService.selectPatientDiseaseRelationByPatientId(patientList.get(i).getId());
            String[] diseaseNames = new String[list.size()];
            Long[] diseaseIds = new Long[list.size()];
            for (int j = 0; j < list.size(); j++) {
                diseaseNames[j] = list.get(j).getName();
                diseaseIds[j] = list.get(j).getDiseaseId();
            }
            patientList.get(i).setDisease(diseaseNames);
            patientList.get(i).setDiseaseIds(diseaseIds);
        }
        return patientList;
    }

    @Override
    public List<PatientRelativesVO> getFamilyInfoById(Long id) {
        return patientMapper.getFamilyInfoById(id);
    }

    @Override
    public int addFamilyMember(PatientRelativesParam patientRelativesParam) {
        return patientMapper.addFamilyMember(patientRelativesParam);
    }


    /**
     * 新增病人信息
     * 
     * @param patient 病人信息
     * @return 结果
     */
    @Override
    public int insertPatient(PatientDTO patientDTO)
    {
        Patient patient = new Patient();
        patient.setName(patientDTO.getName());
        patient.setIdCardNumber(patientDTO.getIdcard());
        patient.setGender(patientDTO.getGender());
        patient.setBirthDate(patientDTO.getBirthDate());
        patient.setAge(patientDTO.getAge());
        patient.setPhoneNumber(patientDTO.getPhone());
        patient.setAddress(patientDTO.getAddress());
        return patientMapper.insertPatient(patient);
    }

    /**
     * 修改病人信息
     * 
     * @param patient 病人信息
     * @return 结果
     */
    @Override
    public int updatePatient(PatientDTO patientDTO)
    {
        Patient patient = new Patient();
        patient.setId(patientDTO.getId());
        patient.setName(patientDTO.getName());
        patient.setIdCardNumber(patientDTO.getIdcard());
        patient.setGender(patientDTO.getGender());
        patient.setBirthDate(patientDTO.getBirthDate());
        patient.setAge(patientDTO.getAge());
        patient.setPhoneNumber(patientDTO.getPhone());
        patient.setAddress(patientDTO.getAddress());
        return patientMapper.updatePatient(patient);
    }

    /**
     * 批量删除病人信息
     * 
     * @param ids 需要删除的病人信息主键
     * @return 结果
     */
    @Override
    public int deletePatientByIds(Long[] ids)
    {
        return patientMapper.deletePatientByIds(ids);
    }

    /**
     * 删除病人信息信息
     * 
     * @param id 病人信息主键
     * @return 结果
     */
    @Override
    public int deletePatientById(Long id)
    {
        return patientMapper.deletePatientById(id);
    }
}
